
/*
 * Copyright (c) 2014. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package com.dc.gameserver.serverCore.controller.handleController.LoginController;

import com.dc.gameserver.hardCode.LoginStatusCode;
import com.dc.gameserver.hardCode.SessionStatus;
import com.dc.gameserver.proto.adminserverprotoVo.LoginRequest;
import com.dc.gameserver.proto.adminserverprotoVo.LoginResponse;
import com.dc.gameserver.proto.adminserverprotoVo.roleVo;
import com.dc.gameserver.proto.itemserverprotoVo.goodsResponse;
import com.dc.gameserver.serverCore.controller.AbstractController.AbstractController;
import com.dc.gameserver.serverCore.controller.AbstractController.IController;
import com.dc.gameserver.serverCore.model.dbsql.modelCache.ModelCacheService;
import com.dc.gameserver.serverCore.model.dbsql.modelService.PlayerService;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TRole;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TUser;
import com.dc.gameserver.serverCore.service.character.PlayerInstance;
import com.dc.gameserver.serverCore.service.managerService.PlayerManagerService;
import com.google.protobuf.MessageLite;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author : 石头哥哥
 *         Project : LandlordsServer
 *         Date: 13-6-4
 *         Time: 上午11:49
 *         Connect: 13638363871@163.com
 *         packageName: Server.ServerCore.Controller
 *      当前 登录模块暂时处理。独立于游戏模块
 *
 */
@Service
public class loginController extends AbstractController {


    private static final Logger LOGGER=Logger.getLogger(loginController.class);

    @Resource
    private PlayerService playerService;

    @Resource
    private ModelCacheService modelCacheService;
    /**
     */
    @Override
    public void PostConstruct() {
       int ID= LoginRequest.msgID.ID_VALUE;
       GAME_CONTROLLERS[ID]=this;   //业务逻辑处理引用
       IController.MESSAGE_LITE[ID]= LoginRequest.getDefaultInstance();   //解析的pb实体类型
    }

     /**
     * messageLite数据结构体分发
     *
     * @param messageLite
     * @param player
     * @throws Exception
     */
    @Override
    public void DispatchMessageLit(MessageLite messageLite, PlayerInstance player) throws Exception {
        //验证结果 1 成功； 2.用户或密码错误； 3.服务器繁忙，请稍候再试；4.账户冻结； 5.帐号在线中；  6.帐号不存在; 7.角色不存在；
        LoginRequest loginRequest= (LoginRequest) messageLite;
        String password=loginRequest.getPassword(); //密码
        String username=loginRequest.getUsername();//用户名
        loginRequest=null;//递交给GC 直接处理
        TUser user=playerService.getUserByAccount(username,password);
        /**用户名 密码验证 这里考虑到后期接入adminserver 暂时不处理*/
        LoginResponse.Builder response_builder=LoginResponse.newBuilder();
        if (user!=null){
            player.setSessionStatus(SessionStatus.ONLINE);
            player.setUserID(user.getUserid());    // userID
            response_builder.setResult(LoginStatusCode.SUCCESS.getStatus());
            TRole tRole=playerService.getRoleByUserID(user.getUserid());

            //返回已经创建的角色,如果存在
            if (tRole!=null){
                player.setID(tRole.getRoleid());
                String role_prefix=ModelCacheService.ROLE_PREFIX(player.getID());

                player.setName(tRole.getRolename());


                //加载玩家初始化信息
                goodsResponse response=playerService.IntiPlayerInfo(player,tRole);

                //session 管理
                PlayerManagerService.INTEGER_PLAYER_INSTANCE_MAP.put(player.getID(),player);

                //推送角色信息
                roleVo.Builder _roleVo=roleVo.newBuilder();
                _roleVo.setRoleID(tRole.getRoleid());
                _roleVo.setRoleName(tRole.getRolename());
                _roleVo.setRoleType(tRole.getRoletype());
                _roleVo.setRoleSex(tRole.getRolesex());

                _roleVo.setRoleMaxBlood(tRole.getRolemaxblood());
                _roleVo.setRoleMaxMagic(tRole.getRolemaxmagic());
                _roleVo.setRoleRecoveryMagic(tRole.getRolerecoverymagic());
                _roleVo.setRoleMaxShiled(tRole.getRolemaxshiled());
                _roleVo.setRoleAttack(tRole.getRoleattack());
                _roleVo.setRoleDefence(tRole.getRoledefence());
                _roleVo.setRoleBaseMinMomentum(tRole.getRolebaseminmomentum());
                _roleVo.setRoleBaseMaxMomentum(tRole.getRolebasemaxmomentum());
                _roleVo.setRoleMaxSual(tRole.getRolemaxsual());
                _roleVo.setRoleSualRecovery(tRole.getRolesualrecovery());
                _roleVo.setRoleExperiences(tRole.getRoleexperiences());
                _roleVo.setRoleLingshiNum(tRole.getRolelevel());
                _roleVo.setRoleMurder(tRole.getRolemurder());
                _roleVo.setRoleMurderLevel(tRole.getRolemurderlevel());
                _roleVo.setRoleLuck(tRole.getRoleluck());
                _roleVo.setRoleLingshiNum(tRole.getRolelingshinum());
                _roleVo.setRoleXianYuNum(tRole.getRolexianyunum());
                _roleVo.setRoleLevel(tRole.getRolelevel());

                 Date date=new Date();
                _roleVo.setRoleCreatTime(date.getTime()); //角色创建时间
                _roleVo.setRoleSoual(tRole.getRolesual()); //角色当前神识值
                _roleVo.setRoleFiveType(tRole.getRolefivetype()); //角色五行属性

                //境界 提升
                _roleVo.setRoleSmalState(tRole.getRolesmalstate());//角色小境界
                _roleVo.setRoleMaxState(tRole.getRolemaxstate());//角色大境界

                _roleVo.setRoleLingQi(tRole.getRolestateexperies());//角色当前灵气
                _roleVo.setRoleLingQILimit(tRole.getStorelingqilimit());//角色灵气存储上限

                double minCount= (tRole.getRolebaseminmomentum()+tRole.getRoleexperiences()/Math.pow(0xa,tRole.getRolesmalstate()));
                double maxCount=(tRole.getRolebasemaxmomentum()+tRole.getRolemurder()*(0xa-(tRole.getRolemaxstate()-1))/Math.pow(0x64,tRole.getRolemaxstate()));

                //气势值---产生点数
                _roleVo.setRoleMaxMomentum((int)maxCount);
                _roleVo.setRoleMinMomentum((int)minCount);
                date=null;
                response_builder.addRoleList(_roleVo.build());

                player.DirectSend(wrappedBufferShort(LoginResponse.msgID.ID_VALUE,response_builder.build()));

                // 推送初始化信息
                player.DirectSend(wrappedBufferShort(goodsResponse.msgID.ID_VALUE, response));

                //推送神识恢复数据
                if (player.autoRecoverySoul(tRole)){
                    playerService.updateRole(tRole);
                }
                //add cache
                modelCacheService.addObjectPrefixID(role_prefix,tRole);
            }else {
                response_builder.addAllRoleList(new ArrayList<roleVo>());
                player.DirectSend(wrappedBufferShort(LoginResponse.msgID.ID_VALUE,response_builder.build()));
            }
        }

    }
}
